﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;

namespace BVVD_Website.userRight
{
    public partial class frmGroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvGroup.DataSource = getData();
                grvGroup.DataBind();

            }
        }

        protected void grvGroup_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvGroup.DataSource = getData();
        }

        private List<Business.LST_NHOMNSD> getData()
        {
            BVVDDataStore<LST_NHOMNSD> db = new BVVDDataStore<LST_NHOMNSD>();
            return db.getFromDB(new Hashtable());
        }

        protected void grvGroup_ItemDataBound(object sender, GridItemEventArgs e)
        {            

            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;
                RadTextBox txtMANHOM = editFormItem.FindControl("txtMANHOM") as RadTextBox;
                
                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    txtMANHOM.ReadOnly = false;
                }
                else
                {                    
                    txtMANHOM.ReadOnly = true;
                }                
            }

        }

        protected void grvGroup_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            string manhom = item.OwnerTableView.DataKeyValues[item.ItemIndex]["MANHOM"].ToString();

            if (manhom.ToUpper() == "admin".ToUpper())
            {
                rnAlert.Text = "Nhóm ADMIN không thể xóa!";
            }
            else
            {
                BVVDDataContext db = new BVVDDataContext();
                LST_NHOMNSD group = (from dm in db.LST_NHOMNSDs
                                where dm.RECID.ToString().Equals(recid)
                                select dm).SingleOrDefault();
                db.LST_NHOMNSDs.DeleteOnSubmit(group);
                try
                {
                    db.SubmitChanges();
                    rnAlert.Text = "Xóa thành công nhóm có mã : " + group.MANHOM;
                }
                catch (Exception ex)
                {
                    rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
                }
            }
            rnAlert.Show();
        }

        protected void grvGroup_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            LST_NHOMNSD group = new LST_NHOMNSD
            {
                MANHOM = (insertedItem.FindControl("txtMANHOM") as RadTextBox).Text,
                TENNHOM = (insertedItem.FindControl("txtTENNHOM") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_NHOMNSDs.InsertOnSubmit(group);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới nhóm thành công  ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }
            rnAlert.Show();
        }

        protected void grvGroup_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_NHOMNSD group = (from item in db.LST_NHOMNSDs
                            where item.RECID.ToString().Equals(recid)
                            select item).SingleOrDefault();

            group.TENNHOM = (editedItem.FindControl("txtTENNHOM") as RadTextBox).Text;
            group.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            group.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công nhóm có mã : " + group.MANHOM;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }
            rnAlert.Show();
        }
    }
}